package com.wtwd.campus.vcom.service.impl;

import com.wtwd.campus.service.RedisService;
import com.wtwd.campus.vcom.common.Constant;
import com.wtwd.campus.vcom.common.RequestId;
import com.wtwd.campus.vcom.dao.RequestRecordMapper;
import com.wtwd.campus.vcom.entity.VcomRequestRecord;
import com.wtwd.campus.vcom.service.RequestRecordService;
import com.wtwd.campus.vcom.utils.CommandNumberUtils;
import com.wtwd.campus.vcom.utils.CommandSendUtils;
import com.wtwd.campus.vcom.utils.RequestIdUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: ldaoliang
 * @DateTime: 2022/1/6 10:43
 * @Description:
 */
@Service
public class RequestRecordServiceImpl implements RequestRecordService {

    @Autowired
    RedisService redisService;
    @Autowired
    RequestRecordMapper requestRecordMapper;

    @Autowired
    CommandSendUtils commandSendUtils;;

    @Override
    public RequestId saveRequestRecord(String imei, String data, int type) {
        // 获取requestId
        String requestId = RequestIdUtils.generateRequestId(redisService, 1);
        String commandNumber = CommandNumberUtils.generateCommandNumber(redisService, imei);
        int i = requestRecordMapper.saveRequestRecord(requestId, commandNumber, imei, data, type, System.currentTimeMillis());
        return i > 0 ? new RequestId(true, requestId) : new RequestId(false, null);
    }

    @Override
    public VcomRequestRecord getNextSendCommand(String imei, String requestId, int type) {
        VcomRequestRecord oldestSending = requestRecordMapper.getOldestSendingCommand(imei, type);
        if (oldestSending == null) {
            VcomRequestRecord oldestNotSend = requestRecordMapper.getOldestNotSendCommand(imei, type);
            if (oldestNotSend != null) {
                requestRecordMapper.updateCommandSendStatus(oldestNotSend.getId(), Constant.STATUS_SEND);
            }
            return oldestNotSend;
        }
        return null;
    }

    @Override
    public RequestId batchCommandSaveRequest(List<String> imeiList, String data, int type) {
        // 获取requestId
        String requestId = RequestIdUtils.generateRequestId(redisService, 1);
        List<VcomRequestRecord> vcomRequestRecords = new ArrayList<>();
        Long currTime = System.currentTimeMillis();
        // 遍历设置值
        for (String imei : imeiList) {
            String commandNumber = CommandNumberUtils.generateCommandNumber(redisService, imei);
            VcomRequestRecord vcomRequestRecord = new VcomRequestRecord(imei, type, commandNumber, requestId, currTime, data);
            vcomRequestRecords.add(vcomRequestRecord);
        }
        if (vcomRequestRecords != null && vcomRequestRecords.size() > 0) {
            int i = requestRecordMapper.batchSaveRequestRecord(vcomRequestRecords);
            return i > 0 ? new RequestId(true, requestId) : new RequestId(false, null);
        }
        return new RequestId(false, null);
    }

    @Override
    public Map<String, Object> getRequestRecordByCondition(String imei, String commandNumber, int type) {
        return requestRecordMapper.selectRequestId(imei, commandNumber, type);
    }

    @Override
    public Boolean updateVcomRequestStateById(int id, Integer state) {
        Integer rest = requestRecordMapper.updateVcomRequestStateById(id, state);
        return rest > 0 ? true : false;
    }

    @Override
    public Integer updateVcomRequestStateByTypesAndImei(String imei, List<Integer> types, List<Integer> states, int state) {
        return requestRecordMapper.updateVcomRequestStateByTypesAndImei(imei, types, states, state);
    }

    @Override
    public boolean CompleteAllInstructions(String imei) {
        if (StringUtils.isBlank(imei)) {
            return false;
        }
        return requestRecordMapper.CompleteAllInstructions(imei, Constant.STATUS_FAILED) > 0 ? true : false;
    }

    @Override
    public boolean CompleteInstructionsByType(String imei, int type) {
        ArrayList<Integer> status = new ArrayList<>();
        status.add(Constant.STATUS_NOT_SEND);
        status.add(Constant.STATUS_SEND);
        return requestRecordMapper.CompleteInstructionsByType(imei, type, Constant.STATUS_COMPLETE, status) > 0 ? true : false;
    }

    @Override
    public RequestId factoryDataReset(String imei) {
        //完成上面改设备所有恢复出厂设置的指令
        CompleteInstructionsByType(imei, Constant.COMMAND_FACTORY_RESET);
        String requestId = RequestIdUtils.generateRequestId(redisService, 1);
        String commandNumber = CommandNumberUtils.generateCommandNumber(redisService, imei);
        boolean rest = sendDeviceReset(imei, requestId, commandNumber);
        return new RequestId(rest, requestId);
    }

    @Override
    public boolean sendDeviceReset(String imei, String requestId, String commandNumber) {
        boolean rest = sendCommanding(imei, requestId, imei, Constant.COMMAND_FACTORY_RESET, commandNumber);
        if (rest) {
            return commandSendUtils.reSetDevice(imei, commandNumber);
        } else {
            return false;
        }
    }

    @Override
    public boolean sendCommanding(String imei, String requestId, String data, Integer type, String commandNumber) {
        return requestRecordMapper.sendCommanding(requestId, commandNumber, imei, data, type, System.currentTimeMillis(), Constant.STATUS_SEND) > 0 ? true : false;
    }


}
